package com.example.dictionary.config;

import com.example.dictionary.service.DictionaryDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 词典委托配置类
 * 配置默认使用 RocksDB，支持多种委托实现同时使用
 */
@Configuration
public class DictionaryDelegateConfig {
    
    private static final Logger log = LoggerFactory.getLogger(DictionaryDelegateConfig.class);
    
    /**
     * 主词典委托 Bean（默认使用 RocksDB）
     * 通过 @Primary 注解，使其成为默认注入的委托
     */
    @Bean
    @Primary
    @ConditionalOnProperty(
            name = "dictionaries.storage.type",
            havingValue = "rocksdb",
            matchIfMissing = true // 默认使用 RocksDB
    )
    public DictionaryDelegate primaryDictionaryDelegate(
            @Qualifier("rocksDBDictionaryDelegate") DictionaryDelegate rocksDBDelegate) {
        log.info("配置主词典委托为 RocksDB");
        return rocksDBDelegate;
    }
    
    /**
     * 备用配置：如果 storage.type 不是 rocksdb，可以使用 SQLite 委托
     */
    @Bean
    @Primary
    @ConditionalOnProperty(
            name = "dictionaries.storage.type",
            havingValue = "sqlite",
            matchIfMissing = false
    )
    public DictionaryDelegate sqliteDictionaryDelegate(
            @Qualifier("dictionaryDelegateImpl") DictionaryDelegate sqliteDelegate) {
        log.info("配置主词典委托为 SQLite");
        return sqliteDelegate;
    }
    
    /**
     * 多委托代理 Bean（可选，如果启用了 multi-delegate）
     * 允许同时使用多个委托实现
     */
    @Bean
    @ConditionalOnProperty(
            name = "dictionaries.storage.multi-delegate",
            havingValue = "true",
            matchIfMissing = false
    )
    public DictionaryDelegate multiDictionaryDelegate(
            @Autowired @Qualifier("multiDictionaryDelegate") DictionaryDelegate multiDelegate) {
        log.info("启用多委托模式");
        return multiDelegate;
    }
}

